// pages/user/giveCoupon/giveCoupon.js
var WxParse = require('../../../wxParse/wxParse.js');
var app = getApp();
var pageSize = 20;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeType: 1,
    showPop: false, //规则弹窗

    couponList: [],//优惠券列表
    recordList: [],//赠送记录

    page: 1,
    empty: true,
    hasMore: false,
    loading: false,
    loadShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRule();
    if (this.data.activeType == 1) {
      this.getInstallCoupon();
    } else {
      this.getSendRecord();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    // 当没有数据时,不再请求
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }

    var page = that.data.page + 1;
    that.setData({
      page: page,
      loading: true
    })
    that.getInstallCoupon();
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '我分享给您1张优惠券，快来领取吧！',
        imageUrl: '/pages/subpackage/images/extend-collectcouponsImg.png',
        path: '/pages/user/collectCoupon/collectCoupon?id=' + res.target.dataset.id + '&user_id=' + wx.getStorageSync('user_id'),
      }
    }
  },

  //切换优惠券、赠送记录列表
  checkType(e){
    var type = e.currentTarget.dataset.type;
    this.setData({
      activeType: type,
      couponList: [],//优惠券列表
      recordList: [],//赠送记录
      page: 1,
      empty: true,
      hasMore: false,
      loading: false,
      loadShow: false,
    })
    if (type == 1) {
      this.getInstallCoupon();
    } else {
      this.getSendRecord();
    }
  },
  //展开使用规则弹窗
  showPop(){
    this.setData({
      showPop: true
    })
  },
  //关闭使用规则弹窗
  closePop() {
    this.setData({
      showPop: false
    })
  },

  //赠送记录加载更多
  loadmore(){
    console.log(123)
    var that = this;
    // 当没有数据时,不再请求
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }

    var page = that.data.page + 1;
    that.setData({
      page: page,
      loading: true
    })
    that.getSendRecord();
  },

  //获取使用规则
  getRule(){
    var that = this;
    app.ajax({
      url: 'Common/Article/getArticle',
      method: "POST",
      data: {
        cate_sn: "AIsQ9NDm"
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var content = res.data.data[0].content;
          WxParse.wxParse('article', 'html', content, that, 5);
        }
      }
    })
  },

  // 获取优惠券列表
  getInstallCoupon() {
    var that = this;
    app.ajax({
      url: 'User/Coupon/getInstallCoupon',
      method: "POST",
      data: {
        page: that.data.page,
        pageSize: pageSize
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (that.data.page == 1) {
            that.setData({
              couponList: res.data.data.data,
              empty: false
            });
          } else {
            that.setData({
              couponList: that.data.couponList.concat(res.data.data.data),
              empty: false
            });
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.data.length < pageSize) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
        } else {
          if (that.data.page == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          loadShow: true
        })
      }
    })
  },
  // 获取赠送记录
  getSendRecord() {
    var that = this;
    app.ajax({
      url: 'User/Coupon/getSendRecord',
      method: "POST",
      data: {
        page: that.data.page,
        pageSize: pageSize
      },
      success: function (res) {
        if (res.data.code == 1000) {
          if (that.data.page == 1) {
            that.setData({
              recordList: res.data.data.data,
              empty: false
            });
          } else {
            that.setData({
              recordList: that.data.recordList.concat(res.data.data.data),
              empty: false
            });
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.data.length < pageSize) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
        } else {
          if (that.data.page == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          loadShow: true
        })
      }
    })
  },
})